<!-- 运行台账-异常点检记录 -->
<template>
  <div class="equip-wrap-common">
    <px-form style="padding-top: 16px" class="custom-form-ui equip-form" :model="queryParams" label-position="right" label-width="120px">
      <px-row :gutter="16">
        <px-col :span="8">
          <px-form-item label="点检工单编号">
            <px-input
              v-model="queryParams.taskNo"
              placeholder="请输入点检工单编号"
              maxlength="50"
              clearable
              @blur="queryParams.taskNo = $event.target.value.trim()"
              @keyup.enter="queryTable"
            >
              <template #suffix>
                <px-icon style="cursor: pointer" @click="queryTable">
                  <search />
                </px-icon>
              </template>
            </px-input>
          </px-form-item>
        </px-col>
        <px-col :span="8">
          <px-form-item label="点检日期">
            <px-date-picker
              v-model="queryParams.taskDate"
              type="daterange"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              unlink-panels
              @change="changeDate"
            />
          </px-form-item>
        </px-col>
        <px-col :span="6">
          <px-button type="primary" @click="queryTable">查询</px-button>
          <px-button @click="resetTable">重置</px-button>
        </px-col>
      </px-row>
    </px-form>
    <px-table :data="tableData" class="equip-table" size="small" height="calc(100vh - 368px)" border>
      <px-table-column
        v-for="(item, index) in tableColumns"
        :key="index"
        :label="item.label"
        :min-width="item.minWidth"
        :align="item.align"
        :show-overflow-tooltip="item.showOverflowTooltip"
        :fixed="item.fixed"
      >
        <template v-if="item.type === 'status'" #default="scope">
          <div class="status">
            <div class="circle" :style="{ backgroundColor: `${tagBgColorList[scope.row.taskStatus]}` }" />
            <p :style="{ color: `${tagBgColorList[scope.row.taskStatus]}` }">{{ scope.row.taskStatusDesc }}{{ scope.row.timeOut ? "(超期)" : "" }}</p>
          </div>
        </template>
        <template v-else-if="item.type === 'link'" #default="scope">
          <span style="color: #3b82f6; cursor: pointer" @click="handleView(scope.row)">
            {{ scope.row.taskNo }}
          </span>
        </template>
        <template v-else #default="scope">{{
          scope.row[item.prop] === 0 ? scope.row[item.prop] : scope.row[item.prop] ? scope.row[item.prop] : "-"
        }}</template>
      </px-table-column>
      <px-table-column fixed="right" label="操作" width="120" align="center">
        <template #default="scope">
          <px-tooltip content="详情" placement="top">
            <px-button link type="primary" icon="Tickets" @click="handleView(scope.row)" />
          </px-tooltip>
        </template>
      </px-table-column>
    </px-table>
    <pagination
      v-show="total > 0"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      class="equip-table-pagination"
      :total="total"
      @pagination="queryTable"
    />
  </div>
</template>

<script setup lang="ts">
import { getExceptionTaskList } from "@/api/equip/runManage";
import { onMounted, reactive, toRefs } from "vue";
import { useRouter } from "vue-router";

const router = useRouter();

const props = defineProps(["eqpId"]);
const tagBgColorList = {
  0: "#909399", // 取消
  1: "#F59E0B", // 待接单
  2: "#3B82F6", // 执行中
  3: "#10B981" // 已完成
};
const state = reactive<any>({
  queryParams: {
    pageNum: 1,
    pageSize: 20
  },
  tableColumns: [
    { prop: "taskNo", label: "任务（工单）编号", minWidth: "150", type: "link", showOverflowTooltip: true, fixed: "left" },
    { prop: "planNo", label: "所属计划编号", minWidth: "150", showOverflowTooltip: true, fixed: "left" },
    { prop: "eqpId", label: "设备编号", minWidth: "180", showOverflowTooltip: true },
    { prop: "eqpName", label: "设备名称", minWidth: "150", showOverflowTooltip: true },
    { prop: "startTime", label: "开始时间", minWidth: "150", showOverflowTooltip: true },
    { prop: "classNameDesc", label: "科室", minWidth: "80", showOverflowTooltip: true },
    { prop: "systemCodeDesc", label: "系统", minWidth: "120", showOverflowTooltip: true },
    { prop: "taskStatusDesc", label: "状态", minWidth: "100", type: "status", showOverflowTooltip: true },
    { prop: "groupCodeName", label: "点检执行组", minWidth: "120", showOverflowTooltip: true },
    { prop: "executorName", label: "执行人", minWidth: "120", showOverflowTooltip: true }
  ],
  tableData: [],
  total: 0
});
const { queryParams, tableColumns, tableData, total } = toRefs(state);

// 进入页面时触发
onMounted(async () => {
  queryTable();
});

// 获取开始日期及结束日期
const changeDate = val => {
  if (val && val.length > 0) {
    queryParams.value.startTime = val[0] + " 00:00:00";
    queryParams.value.endTime = val[1] + " 23:59:59";
  } else {
    queryParams.value.startTime = "";
    queryParams.value.endTime = "";
  }
};
// 查询列表
const queryTable = async () => {
  queryParams.value.eqpId = props.eqpId;
  const params = JSON.parse(JSON.stringify(queryParams.value));
  const data = await getExceptionTaskList(params);
  tableData.value = data?.data || [];
  total.value = data?.total || 0;
};
// 重置筛选条件列表
const resetTable = async () => {
  queryParams.value = {
    eqpId: props.eqpId,
    pageNum: queryParams.value.pageNum,
    pageSize: queryParams.value.pageSize
  };
  queryTable();
};
// 详情
const handleView = (row: any) => {
  router.push(`/all/equip/maintain/exception/task?planNo=${row.planNo}&taskNo=${row.taskNo}&taskStatus=${row.taskStatus}`);
};
</script>

<style lang="scss" scoped>
@import url("@/views/all/equip/scss/table-list.scss");

.equip-wrap-common {
  overflow-x: hidden;
  height: calc(100vh - 231px);
  padding: 0;
  .status {
    display: flex;
    flex-direction: row;
    align-items: center;

    .circle {
      width: 8px;
      height: 8px;
      margin-right: 5px;
      border-radius: 50%;
    }
  }
}
</style>
